package testndk.testservice;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

import testndk.testservice.service.CustomInfaceService;

public class CustomInActivity extends Activity {
	private boolean mIsBind;
	private Button startBtn = null;
	private Button stopBtn = null;
	private TextView mTextView = null;
	private Intent intent = null;
	private ProgressBar mProgressBar = null;
	private CustomInfaceService.ServiceBinder serviceBinder;

	/** 计时器*/
	private Timer timer;

	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what){
				case 21:
					int curLoad = serviceBinder.getCurrentLoad();
					mTextView.setText(curLoad+"%");
					mProgressBar.setProgress(curLoad);
					break;
			}
		}
	};

	/** 服务练级监听*/
	private ServiceConnection serConn = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName name) {
			serviceBinder = null;
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			serviceBinder = (CustomInfaceService.ServiceBinder) service;
			if (serviceBinder != null){
				timer.schedule(new MyTimerTask(), 0, 1000);
			}
		}
	};

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
		timer = new Timer();

		intent = new Intent(this,CustomInfaceService.class);
        mProgressBar = (ProgressBar)findViewById(R.id.myProgressBar);
        mProgressBar.setMax(100);
        mTextView = (TextView)findViewById(R.id.loading_Tv);
        startBtn = (Button)findViewById(R.id.start_Btn);
        stopBtn = (Button)findViewById(R.id.stop_Btn);
        startBtn.setOnClickListener(new ButtonClickListener());
        stopBtn.setOnClickListener(new ButtonClickListener());
        
    }
	class ButtonClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			if (startBtn == v) {
				mIsBind = bindService(intent, serConn, Context.BIND_AUTO_CREATE);//if bind success return true
			} else if (stopBtn == v) {
				if(mIsBind){
					unbindService(serConn);
//					timer.cancel();
					mIsBind = false;
				}
			}
		}

	}


	/**计时器*/
	class MyTimerTask extends TimerTask {
		@Override
		public void run() {
			Message message = handler.obtainMessage();
			message.what = 21;
			handler.sendMessage(message);
		}
	}


	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (timer != null){
			timer.cancel();
		}
		if (mIsBind){
			unbindService(serConn);
		}
	}
}